#include <SFML/Graphics.hpp>
#include <sfFlashControl.h>

bool running = true;
sf::Color background(0,255,0);

class TestControl: public sf::FlashControl
{
public:
	TestControl(int width, int height)
		:sf::FlashControl(width,height)
	{
	}

	virtual SwfUI::Argument onFlashCall(const std::wstring& funcName, const SwfUI::ArgumentList& args)
	{
		if(funcName == L"exitClick")
			running = false;
		else if(funcName == L"colorChange")
			background = args[0]; //this is where the custom converter kicks in
		else if(funcName == L"opacityChange")
			SetColor(sf::Color(255,255,255,(int)(02.55f *(float)args[0])));
		
		return SwfUI::Argument::Null;
	}
};

struct SwfUIInitializer
{
	SwfUIInitializer()
	{
		SwfUI::initialize();
	}

	~SwfUIInitializer()
	{
		SwfUI::shutdown();
	}
};

int main()
{
	SwfUIInitializer init;
	{
		using namespace sf;

		SwfUI::Argument a = sf::Color::Blue;

		TestControl test(512,512);
		test.load(L"controls.swf");
		test.SetPosition(100.0f,200.0f);
		test.SetRotation(45.0f);
		test.setRenderQuality(SwfUI::RQ_AUTOHIGH);
		test.setTransparent(true);
		SwfUI::setFocus(&test);//let this control receive keyboard input

		FlashControl fps(128,128);
		fps.load(L"fps.swf");
		fps.setTransparent(true);
		fps.setRenderQuality(SwfUI::RQ_AUTOLOW);

		sf::RenderWindow window(sf::VideoMode(800,600),"Test");
		SwfUI::ArgumentList args;
		const sf::Input& input = window.GetInput();

		while(window.IsOpened() && running)
		{
			sf::Event ev;
			while(window.GetEvent(ev))
			{
				if(ev.Type==sf::Event::Closed)
					window.Close();

				switch(ev.Type)
				{
				case sf::Event::MouseButtonPressed:
					test.injectMouse(SwfUI::MET_MOUSELDOWN,ev.MouseButton.X,ev.MouseButton.Y,0);
					break;
				case sf::Event::MouseButtonReleased:
					test.injectMouse(SwfUI::MET_MOUSELUP,ev.MouseButton.X,ev.MouseButton.Y,0);
					break;
				case sf::Event::MouseMoved:
					test.injectMouse(SwfUI::MET_MOUSEMOVED,ev.MouseMove.X,ev.MouseMove.Y,0);
					break;
				case sf::Event::MouseWheelMoved:
					test.injectMouse(SwfUI::MET_MOUSEWHEEL,input.GetMouseX(),input.GetMouseY(),ev.MouseWheel.Delta);
					break;
				}
			}

			args.clear();
			if(window.GetFrameTime() > 0.0f)
				args(1.0f / window.GetFrameTime());
			else
				args(0);

			fps.callFunction(L"setFPS",args);			
			sf::FlashControl::updateAll();
			window.Clear(background);
			window.Draw(test);
			window.Draw(fps);
			window.Display();
		}
	}
}